<?php
namespace app\hongyanche\model;

// use think\Db;
// Db::listen(function($sql, $time, $explain){
//     // 记录SQL
//     echo $sql. ' ['.$time.'s]';
//     // 查看性能分析结果
//     dump($explain);
// });
// use think\Model;
use Composer\Package\Package;

class Expression
{
    private $user;
    private $user_id;
    private $isMarketingAdmin;

    function __construct()
    {
        $user = $this->user = model('user');
        $this->user_id = $user->getUserId();
        $admin = model('admin')->is('marketing');
        $this->isMarketingAdmin = isset($admin['state']) ? $admin['state'] : 0;
    }

    //左侧导航
    public function add($imgurl)
    {
        if (!$this->user_id) return array('state' => 0, 'errormsg' => '请登录');
        if (!$this->isMarketingAdmin) return ['state' => 0, 'errormsg' => '您没有权限'];
        $range = db('expression')->where('state = 1')->order('`range` desc')->find();
        if ($range) {
            $data['range'] = $range['range'] + 1;
        } else {
            $data['range'] = 1;
        }
        //获取等级
        $limit = db('expression')->where('state = 1')->order('`limit` desc')->find();
        $data['create_time'] = time();
        $data['state'] = 1;
        $data['user_id'] = $this->user_id;
        $data['imgurl'] = $imgurl;
        $data['limit'] = $limit['limit'];
        $result = db('expression')->insertGetId($data);
        if (!$result) {
            return array('state' => 0, 'errormsg' => 'insert fail');
        }
//        加入action操作
        model('action')->add('expression_create', 'expression_id', $result);
        return array('state' => 1, 'errormsg' => 'ok', 'errorcode' => '',);
    }

    //开放关闭导航栏
    public function release($expression_id, $release)
    {
        if (!$expression_id > 0) {
            return array('state' => 0, 'errormsg' => 'brand_id incorrect', 'errorcode' => '');
        }
        if ($release == 1) {
            $data['state'] = 1;
        } else {
            $data['state'] = 2;
        }
        $where = "expression_id in($expression_id)";
        $typeconfigstate = db('expression')->where($where)->update($data);
        if ($typeconfigstate) {
            return array('state' => 1, 'errormsg' => 'ok', 'errorcode' => '');
        }
        model('action')->add('expression_release', 'state', $release);
        return array('state' => 0, 'errormsg' => 'save error', 'errorcode' => '');
    }

    //导航删除
    public function remove($expression_id)
    {
        if (!$this->user_id) return array('state' => 0, 'errormsg' => '请登录');
        if (!$expression_id) return array('state' => 0, 'errormsg' => '请选择一个表情');
        $res = db('expression')->where("expression_id in($expression_id)")->update(['state' => 0]);
        // 记录action表动作行为
        if ($res) {
            model('action')->add('expression_remove', 'expression_id', $expression_id);
            $ret = array('state' => 1, 'errormsg' => 'ok');
        } else {
            $ret = array('state' => 0, 'errormsg' => '删除失败');
        }
        return $ret;
    }

    //导航栏列表
    function getlist($data = null)
    {
        $where = ['state' => 1];
        $res = db('expression')
            ->where($where)
            ->order('`range`')
            ->select();
        return ['state' => 1, 'data' => ['list' => $res]];
    }

    //排序
    public function range($ranges)
    {
        if (!$ranges) return ['state' => 0, 'range error'];
        $ranges = explode(",", $ranges);
        foreach ($ranges as $range => $expression_id) {
            $where = ['expression_id' => $expression_id, 'state' => ['<>', 0]];
            $update = ['range' => $range];
            db('expression')->where($where)->update($update);
        }
        model('action')->add('expression_range', 'expression_id', $ranges);
        return ['state' => 1];
    }
    //设置等级
    public function setlevel($level)
    {
        $res = db('expression')->where("state = 1")->update(['limit'=>$level]);
        if(!$res) return ['state'=>0,'errormsg'=>'设置失败'];
        return ['state'=>1,'errormsg'=>'设置成功'];
    }
}


